<?php

class QuoteController extends BaseClass
{
	public $title;	//Page Title
	public $layout = 'main';

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
						'actions' => array('index', 'view', 'delete', 'process'),
						'users' => $this->getAdmins(),
				),
			array('deny', // deny other user to perform actions
					'actions' => array('index', 'view', 'delete', 'process'),
					'users' => array('*'),
			),
		);
	}
	
	/*
	 * Display list of contacts in grid
	*/
	public function actionIndex()
	{
		$model = new Quotes('search');
		$model->unsetAttributes();  // clear any default values
		$this->title = Yii::app()->name.'::Free-Quotes';
		$this->render('index', array('model'=>$model,));
	}
	
	/*
	 * Display view page per person detail
	 */
	public function actionView($id) {
		$model = $this->loadModel($id);
		$this->title = Yii::app()->name.'::View-Quotes';
		$this->render('view', array('model'=>$model));
	}
	
	/*
	 * Delete Contact
	*/
	public function actionDelete($id) {
		$model = $this->loadModel($id);
		if($model->delete()) return true;
		else return false;
	}
	/*
	 * Bulk deletion of contacts
	 */
	public function actionProcess(){
		$model = new Quotes();
		if($_POST['pageAction'] == 'delete')
			{
				if($model->deleteByPk($_POST['contact'])) {
					Yii::app()->user->setFlash('success','Record (s) Successfully Deleted');
					$this->redirect(Yii::app()->getBaseUrl().'/admin/quote');
				}
				else {
					Yii::app()->user->setFlash('error','Record (s) not Deleted');
					$this->redirect(Yii::app()->getBaseUrl().'/admin/quote');
				}
			}
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id) {

        $model = Quotes::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }
}